package br.com.agiledev.webscrum.entity;

import static javax.persistence.FetchType.LAZY;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;

import org.hibernate.annotations.ForeignKey;

/**
 * Entity implementation class for Entity: ItemRelease
 * 
 */
@Entity
@SequenceGenerator(name = "itemproduct", sequenceName = "se_itemproduct")
@NamedQueries( { @NamedQuery(name = "ItemProduct.excluirPorItem", query = "delete from ItemProduct ip where ip.item = :item") })
public class ItemProduct implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "itemproduct", strategy = GenerationType.SEQUENCE)
    private Long oid;

    @ManyToOne(fetch = LAZY, optional = false, cascade = { CascadeType.REMOVE })
    @ForeignKey(name = "fk_itemproduct_item")
    private Item item;

    @ManyToOne(fetch = LAZY, optional = false, cascade = { CascadeType.REMOVE })
    @ForeignKey(name = "fk_itemproduct_product")
    private Product product;

    public ItemProduct() {
	super();
    }

    public ItemProduct(Item item, Product product) {
	setItem(item);
	setProduct(product);
    }

    public Item getItem() {
	return item;
    }

    public Long getOid() {
	return oid;
    }

    public Product getProduct() {
	return product;
    }

    public void setItem(Item item) {
	this.item = item;
    }

    public void setOid(Long oid) {
	this.oid = oid;
    }

    public void setProduct(Product product) {
	this.product = product;
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((item == null) ? 0 : item.hashCode());
	result = prime * result + ((oid == null) ? 0 : oid.hashCode());
	result = prime * result + ((product == null) ? 0 : product.hashCode());
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	ItemProduct other = (ItemProduct) obj;
	if (item == null) {
	    if (other.item != null)
		return false;
	} else if (!item.equals(other.getItem()))
	    return false;
	if (product == null) {
	    if (other.product != null)
		return false;
	} else if (!product.equals(other.getProduct()))
	    return false;
	return true;
    }

    @Override
    public String toString() {
	return (item != null ? item.toString() : "null") + ":" + (product != null ? product.toString() : "null");
    }
}